<?php
namespace Admin\Controller;
use Think\Controller;

class ComplainController extends BaseController{
    public function index(){
        $Complainmodel=D('Complain');
        $p=I('get.p')?I('get.p'):1;
        $pnum=10;
        $keyword=I('request.keyword');
        if (isset($keyword)&&!empty($keyword)){
            $map['user_name']=array("like","%".$keyword."%");
            $Usermodel=D('user');
            $user=$Usermodel->where($map)->select();
            $kw=array('keyword'=>$keyword);
            if($user[0]['user_id']){
                for($i=0;$i<count($user);$i++){
                    $arr[]=$user[$i]['user_id'];
                }
                if(count($arr)==1){
                    $where='a.user_id = '.implode(',',$arr);
                }else{
                    $where='a.user_id in('.implode(',',$arr).')';
                }
            }
            else{
                $this->error("查找的用户不存在!",U('Admin/Complain/index/p/'.$p));
            }
        }
        //夏季拔插
        $complains=$Complainmodel
        ->page($p,$pnum)
        ->join('left join tp_user as a ON tp_complain.user_id = a.user_id')
        ->join('left join tp_info as b ON tp_complain.info_id = b.info_id')
        ->order('com_id desc')
        ->where($where?$where:1)
        ->select();
        //如果查出的数组为空并且当前页数不是第一页则跳转到上一页即页数-1
        if(empty($complains)&&$p!=1){
            $p=$p-1;
            $url=U("Admin/Complain/index/p/$p/keyword/$keyword");
            Header("Location:$url");
        }
	    $count=$Complainmodel
        //->page($p,$pnum)
        ->join('left join tp_user as a ON tp_complain.user_id = a.user_id')
        ->join('left join tp_info as b ON tp_complain.info_id = b.info_id')
        ->order('com_id desc')
        ->where($where?$where:1)
	    ->count();
	    //实例化分页对象
	    $page=new \Think\Page($count,$pnum,$p,"",$kw);
	    //获取分页信息
	    $show=$page->show();
	    //发送分页信息到模版
	    $this->assign("keyword",$keyword);
	    $this->assign("pageinfo",$show);
	    //发送查询结果到页面
        $this->assign('coms',$complains);
        $this->assign('p',$p);
        $this->display();
        
        //你瞅啥?
    }
    public function handle(){
        $p=I('get.p');
        $keyword=I('get.keyword');
        $data['com_id']=I('request.id');
        if(is_array($data['com_id'])){
            $where = 'com_id in('.implode(',',$data['com_id']).')';
        }else{
            $where = 'com_id='.$data['com_id'];
        }
        $data['if_dispose']=1;
        $Complainmodel=D('Complain');
	    $result = $Complainmodel->where($where)->save($data);
        $url=$keyword?U("Admin/Complain/index/p/$p/keyword/$keyword"):U("Admin/Complain/index/p/$p");
        Header("Location:$url");
    }
    public function delete(){
        $id=I('request.id');
        $keyword=I('get.keyword');
        $p=I('get.p');
        $Complainmodel=D('Complain');
        if(is_array($id)){
            $where = 'com_id in('.implode(',',$id).')';
        }else{
            $where = 'com_id='.$id;
        }
        $Complainmodel->where($where)->delete();
        $url=$keyword?U("Admin/Complain/index/p/$p/keyword/$keyword"):U("Admin/Complain/index/p/$p");
        Header("Location:$url");

    }
}